//
//  AppDelegate.m
//  RJETGeography
//
//  Created by RJET04 on 2023/8/22.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[HomeViewController new]];
    [self.window makeKeyAndVisible];
    
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

    //打开日志
    [WXApi startLogByLevel:WXLogLevelDetail logBlock:^(NSString * _Nonnull log) {
        
        NSLog(@"wx log: %@", log);
    }];
    
    //向微信注册
    BOOL regRes = [WXApi registerApp:@"wx488966b206225ca7" universalLink:@"https://app.rjet.xyz/"];
    NSLog(@"微信sdk注册结果： %d", regRes);
    
    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
}

//iOS9之前
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    if ([url.host isEqualToString:@"safepay"]) {
        
        //支付宝支付
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                    
            NSLog(@"支付宝支付结果: %@", resultDic);
        }];
    }else{
        
        //微信
        [WXApi handleOpenURL:url delegate:self];
    }
    
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([url.host isEqualToString:@"safepay"]) {
        
        //支付宝支付
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                    
            NSLog(@"支付宝支付结果: %@", resultDic);
        }];
    }else{
        
        //微信
        [WXApi handleOpenURL:url delegate:self];
    }
    
    return YES;
}

//iOS9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        
        //支付宝支付
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
                    
            NSLog(@"支付宝支付结果: %@", resultDic);
        }];
    }else{
        
        //微信
        [WXApi handleOpenURL:url delegate:self];
    }
    
    return YES;
}


//MARK: -- WXApiDelegate
- (void)onReq:(BaseReq *)req
{
    NSLog(@"onrequ: %@", req);
}

//MARK: -- WXApiDelegate
- (void)onResp:(BaseResp *)resp
{
    NSLog(@"onResp: %@", resp);
    if ([resp isKindOfClass:[WXOpenCustomerServiceResp class]]) {
        //微信客服
        int errCode = resp.errCode;
        NSString *errMsg = resp.errStr;
        NSLog(@"打开客服: %d %@", errCode, errMsg);
        
    }else if ([resp isKindOfClass:[PayResp class]]){
        //微信支付
        switch (resp.errCode) {
            case WXSuccess:
                NSLog(@"微信支付成功");
                break;
                
            default:
                NSLog(@"微信支付失败: %d %@", resp.errCode, resp.errStr);
                break;
        }
    }
}



@end
